package com.js.basic.service.impl;

import java.util.*;

import com.js.basic.domain.dto.ExpertInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.js.basic.mapper.BsExpertInfoMapper;
import com.js.basic.domain.BsExpertInfo;
import com.js.basic.service.IBsExpertInfoService;

/**
 * 专家信息Service业务层处理
 *
 * @author lzq
 * @date 20240703
 */
@Service
public class BsExpertInfoServiceImpl implements IBsExpertInfoService {
    @Autowired
    private BsExpertInfoMapper bsExpertInfoMapper;

    /**
     * 查询专家信息
     *
     * @param expertCode 专家信息主键
     * @return 专家信息
     */
    @Override
    public BsExpertInfo selectBsExpertInfoByExpertCode(String expertCode) {
        return bsExpertInfoMapper.selectBsExpertInfoByExpertCode(expertCode);
    }

    /**
     * 查询专家信息列表
     *
     * @param bsExpertInfo 专家信息
     * @return 专家信息
     */
    @Override
    public List<BsExpertInfo> selectBsExpertInfoList(BsExpertInfo bsExpertInfo) {
        return bsExpertInfoMapper.selectBsExpertInfoList(bsExpertInfo);
    }

    /**
     * 新增专家信息
     *
     * @param bsExpertInfo 专家信息
     * @return 结果
     */
    @Override
    public int insertBsExpertInfo(BsExpertInfo bsExpertInfo) {
        return bsExpertInfoMapper.insertBsExpertInfo(bsExpertInfo);
    }

    /**
     * 修改专家信息
     *
     * @param bsExpertInfo 专家信息
     * @return 结果
     */
    @Override
    public int updateBsExpertInfo(BsExpertInfo bsExpertInfo) {
        return bsExpertInfoMapper.updateBsExpertInfo(bsExpertInfo);
    }

    /**
     * 批量删除专家信息
     *
     * @param expertCodes 需要删除的专家信息主键
     * @return 结果
     */
    @Override
    public int deleteBsExpertInfoByExpertCodes(String[] expertCodes) {
        return bsExpertInfoMapper.deleteBsExpertInfoByExpertCodes(expertCodes);
    }

    /**
     * 删除专家信息信息
     *
     * @param expertCode 专家信息主键
     * @return 结果
     */
    @Override
    public int deleteBsExpertInfoByExpertCode(String expertCode) {
        return bsExpertInfoMapper.deleteBsExpertInfoByExpertCode(expertCode);
    }

    @Override
    public List<BsExpertInfo> selectSuccessBsExpertList(ExpertInfoDto expertInfoDto) {
        String[] arr = expertInfoDto.getAddress().split(",");
        List<BsExpertInfo> expertInfos = new ArrayList<>();
        for (String str : arr) {
            expertInfoDto.setAddress(str);

            List<BsExpertInfo> infos = bsExpertInfoMapper.selectSuccessBsExpertList(expertInfoDto);
            for (BsExpertInfo info : infos) {
                expertInfos.add(info);
            }
        }

        return expertInfos;
    }

    @Override
    public List<BsExpertInfo> selectSuccessBsExpertListAdd(ExpertInfoDto expertInfoDto) {

        String[] arr = expertInfoDto.getAddress().split(",");
        List<BsExpertInfo> expertInfos = new ArrayList<>();
        for (String str : arr) {
            expertInfoDto.setAddress(str);
            List<BsExpertInfo> infos = bsExpertInfoMapper.selectSuccessBsExpertList(expertInfoDto);
            for (BsExpertInfo info : infos) {
                expertInfos.add(info);
            }
        }
        int index = expertInfoDto.getNumber();
        Set<BsExpertInfo> set = new HashSet<>(); // 使用 Set 来存储随机选择的元素

        Random random = new Random();
        if (index >= expertInfos.size()) {
            index = expertInfos.size();
        }
        while (set.size() < index) { // 循环直到 Set 中有 index 个元素
            int randomIndex = random.nextInt(expertInfos.size());
            set.add(expertInfos.get(randomIndex)); // 添加随机选择的元素到 Set 中
        }

        List<BsExpertInfo> resultList = new ArrayList<>(set); // 将 Set 转换为 List

        return resultList;
    }


}
